/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceQuestionAnswerDao;
import com.lczyfz.istep.modules.resource.entity.ResourceQuestion;
import com.lczyfz.istep.modules.resource.entity.ResourceQuestionAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 答案表Service
 * @author dh
 * @version 2017-02-21
 */



@Service
@Transactional(readOnly = true)
public class ResourceQuestionAnswerService extends CrudService<ResourceQuestionAnswerDao, ResourceQuestionAnswer> {


	@Autowired
	ResourceQuestionAnswerDao resourceQuestionAnswerDao;

	public ResourceQuestionAnswer get(String id) {
		return super.get(id);
	}
	
	public List<ResourceQuestionAnswer> findList(ResourceQuestionAnswer resourceQuestionAnswer) {
		return super.findList(resourceQuestionAnswer);
	}


	public Page<ResourceQuestionAnswer> findPage(Page<ResourceQuestionAnswer> page, ResourceQuestionAnswer resourceQuestionAnswer) {
		return super.findPage(page, resourceQuestionAnswer);
	}

	public List<ResourceQuestionAnswer> findAnswerList() {

				return resourceQuestionAnswerDao.findAnswerList();
	}
	@Transactional(readOnly = false)
	public void save(ResourceQuestion resourceQuestion,String questionAnswer) {


		String[] answerList = new String[0];

		if(resourceQuestion!=null&&!resourceQuestion.getId().equals(""))
		{
			ResourceQuestionAnswer resourceQuestionAnswer=new ResourceQuestionAnswer();
			resourceQuestionAnswer.setQuestionId(resourceQuestion.getId());
			List<ResourceQuestionAnswer> list=dao.findList(resourceQuestionAnswer);
			for(int i=0;i<list.size();i++)
			{
				dao.delete(list.get(i));
			}

		}
		if (questionAnswer != null && !questionAnswer.equals(""))
			answerList = questionAnswer.split(",");
		logger.debug("length: [{}]", answerList.length);

		for (int i = 0; i < answerList.length; i++) {
			ResourceQuestionAnswer resourceQuestionAnswer =new ResourceQuestionAnswer();
			resourceQuestionAnswer.setAnswer(answerList[i]);
			resourceQuestionAnswer.setQuestionId(resourceQuestion.getId());
			super.save(resourceQuestionAnswer);
		}

	}
	@Transactional(readOnly = false)
	public void delete(ResourceQuestionAnswer resourceQuestionAnswer) {
		super.delete(resourceQuestionAnswer);
	}
	
}